package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.dao.GuruDAO;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("guru")
public class GuruController {
    @Autowired
    private GuruService guruService;
    @Autowired
    private GuruDAO guruDAO;

    @LogAnnotation(value = "查询所有的上师",type = "selectpage")
    @ResponseBody
    @RequestMapping("selectPage")
    @AddCacheAnnotation
    public Map selectPage(Integer page, Integer limit){
        Map m = new HashMap();
        PageInfo<Guru> pageInfo = guruService.selectAll(page, limit);
        m.put("code",0);
        m.put("msg","");
        m.put("count",pageInfo.getTotal());
        m.put("data",pageInfo.getList());
        return m;
    }
    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws Exception {
        String s = UUID.randomUUID().toString()+file.getOriginalFilename();
        file.transferTo(new File("D:\\fuwuqi",s));
        Map m = new HashMap();
        m.put("code",0);
        m.put("pic",s);
        return m;
    }
    @ResponseBody
    @RequestMapping("add")
    public Map add(Guru guru){
        Map m = new HashMap();
        try {
            guruService.insert(guru);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @ResponseBody
    @RequestMapping("update")
    public Map update(Guru guru){
        Map m = new HashMap();
        try {
            guruService.updateStatus(guru);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @ResponseBody
    @RequestMapping("delete")
    public Map delete(Integer guruId){
        Map m = new HashMap();
        try {
            guruService.deleteByPrimaryKey(guruId);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @ResponseBody
    @RequestMapping("uploadsGuru")
    public Map uploadsGuru(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
        //获取最后一行数据的下标
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <=lastRowNum; i++) {
            HSSFRow row = sheet.getRow(i);
            Guru guru  = new Guru();
            double numericCellValue = row.getCell(0).getNumericCellValue();
            guru.setGuruId((int) numericCellValue);
            guru.setGuruName(row.getCell(1).getStringCellValue());
            guru.setGuruImage(row.getCell(2).getStringCellValue());
            guru.setGuruNickname(row.getCell(3).getStringCellValue());
            guru.setGuruStatus((int) row.getCell(4).getNumericCellValue());
            guruService.insert(guru);
        }
        Map m = new HashMap();
        m.put("code",0);
        return m;
    }
    @ResponseBody
    @RequestMapping("download")
    public void download(HttpServletResponse response) throws Exception {
        List<Guru> list = guruDAO.selectAll();
        /**
         * ExcelExportUtil.exportExcel 导出方法
         * 参数1 导出参数对象 title 标题 sheetName 表名
         * 参数2 要导出的实体类的类对象
         * 参数3 要导出的数据 集合
         */
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("所有上师数据", "guru"), Guru.class, list);
        /**
         * easypoi 支持两种格式 xls 和 xlsx
         */
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("guru.xls", "utf-8"));
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        workbook.write(response.getOutputStream());
    }
}
